package com.travel.admin.web.response;


import com.travel.admin.exception.TravelExceptionCode;
import org.apache.commons.lang3.StringUtils;


/**
 *@desc: 统一封装 处理响应类
 */
public class HandleResp<T> extends BaseResp {

    private int code;

    private String msg;

    private T data;

    private String microCode;

    public HandleResp() {
    }

    private HandleResp(TravelExceptionCode zlcpdExceptionCode, T data){
        this.code = zlcpdExceptionCode.getErrorCode();
        if(StringUtils.isNotBlank(zlcpdExceptionCode.getCustomMsg())){
            this.msg = zlcpdExceptionCode.getCustomMsg();
        }else {
            this.msg = zlcpdExceptionCode.getDesc();
        }
        this.data = data;
        this.microCode = zlcpdExceptionCode.getCode();
    }

    private HandleResp(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 封装成功的响应
     * @return
     */
    public static <T> HandleResp<T> successResponse(T data){
        return new HandleResp(TravelExceptionCode.SUCCESS, data);
    }
    /**
     * 封装成功的响应
     * @return
     */
    public static <T> HandleResp<T> successResponse(){
        return new HandleResp(TravelExceptionCode.SUCCESS, null);
    }
    /**
     * 封装失败的响应
     * @return
     */
    public static <T> HandleResp<T> failedResponse(TravelExceptionCode zlcpdExceptionCode, T data){
        return new HandleResp(zlcpdExceptionCode,data);
    }
    /**
     * 封装失败的响应
     * @return
     */
    public static <T> HandleResp<T> failedResponse(TravelExceptionCode zlcpdExceptionCode){
        return new HandleResp(zlcpdExceptionCode,null);
    }
    /**
     * 全局统一异常处理封装失败的响应
     *
     * @return
     */
    public static <T> HandleResp<T> failedResponseForGlobal(int code, String msg, T data) {
        return new HandleResp(code, msg, data);
    }

    /**
     * 全局统一异常处理封装失败的响应
     *
     * @return
     */
    public static <T> HandleResp<T> failedResponseForGlobal(int code, String msg) {
        return new HandleResp(code, msg, null);
    }


    /**
     * 封装失败的响应 环绕异常处理专用
     * @return 失败的响应体
     */
    public static <T> HandleResp<T> failedResponse(int code, String msg){
        return new HandleResp(code, msg, null);
    }

    public static <T> HandleResp<T> failedResponseMsg(String msg){
        return new HandleResp(TravelExceptionCode.FAILED.getErrorCode(), msg, null);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMicroCode() {
        return microCode;
    }

    public void setMicroCode(String microCode) {
        this.microCode = microCode;
    }

}
